<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * question short summary.
 *
 * question description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Question extends Sys_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Question_model');
        $this->load->library('form_validation');
        $this->load->library('ajaxMsgModel');
        $this->load->database();
    }

    public function questionlist()
    {
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        $page_size=10;
        $one=$this->Question_model->get_page($cupge,$page_size,"*","question","order by weight desc, question_id asc","question_id","");
        $this->load->library('page');
        $p=new Page();

        $data['question_List']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$page_size,site_url('admin/question/questionlist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/question/questionlist.html',$data);
    }

    public function addquestion()
    {
        $this->load->view('admin/question/addquestion.html');
    }

    public function add_question()
    {
        $valiRes= $this->form_validation->run('question_vali');
        $this->load->library('ajaxMsgModel');
        $questionModel= new Question_model();
        if($valiRes)
        {
            $questionModel->q_contents=$this->input->post('q_contents');
            $questionModel->a_contents=$this->input->post('a_contents');
            $questionModel->weight=$this->input->post('weight');

            $res= $questionModel->inset_question($questionModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';

            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editquestion()
    {
        $questionid=$this->uri->segment(4);
        $questionModel=$this->Question_model->get_question_by_id($questionid);
        $data['questionModel']=$questionModel;
        $this->load->view('admin/question/editquestion.html',$data);
    }

    public  function edit_question()
    {
        $valiRes= $this->form_validation->run('question_vali');
        $this->load->library('ajaxMsgModel');
        $questionModel= new Question_model();
        if($valiRes)
        {
            $questionModel->question_id=$this->input->post('question_id');
            $questionModel->q_contents=$this->input->post('q_contents');
            $questionModel->a_contents=$this->input->post('a_contents');
            $questionModel->weight=$this->input->post('weight');

            $res= $questionModel->edit_question($questionModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';

            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function set_question_weight()
    {
        $this->load->library('ajaxMsgModel');
        $questionModel=new $this->Question_model();
        $questionModel->question_id=$this->uri->segment(4);
        $questionModel->weight=$this->input->post('weight');
        $backurl=$this->input->get('returnurl');
        $res=$questionModel->Set_question_Weight($questionModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_question()
    {
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $res=$this->Question_model->del_question_byid($this->uri->segment(4));
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batchdel_question()
    {
        $question_ids=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $questionModel=new $this->Question_model();
        $question_idsArray=explode('|',trim($question_ids,'|'));
        $res=0;
        if(count($question_idsArray)>0)
        {
            $res=$questionModel->batchdel_question($question_idsArray);
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}